package com.hrms.hrms_employee.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.common.vo.ResultMessage;
import com.hrms.hrms_employee.entity.StaffDetail;
import com.hrms.hrms_employee.service.StaffDetailService;
import com.hrms.hrms_employee.vo.StaffDetailAttachmentVO;
import com.hrms.hrms_employee.vo.WrapperDTO;
import com.hrms.hrms_employee.vo.staffDetailVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工个人信息表(StaffDetail)表控制层
 *
 * @author makejava
 * @since 2025-01-17 17:14:23
 */
@RestController
@RequestMapping("staffDetail")
public class StaffDetailController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private StaffDetailService staffDetailService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param staffDetail 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page page, StaffDetail staffDetail) {
        return success(this.staffDetailService.page(page, new QueryWrapper<>(staffDetail)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/one")
    public ResultMessage selectDetail(@RequestParam("id") String id) {
        staffDetailVO staffDetailVO = this.staffDetailService.SelectStaffDetail(Long.parseLong(id));
        return new ResultMessage(ResultMessage.Success,"",staffDetailVO);
    }

    /**
     * 新增数据并开启入职审批
     *
     * @param
     * @return 新增结果
     */
    @PostMapping
    public ResultMessage insert(
            @RequestParam Map<String, String> formData,
            @RequestParam("photo") MultipartFile photo, // 单独处理文件
            @RequestParam("contractFile") MultipartFile contractFile,
            @ModelAttribute WrapperDTO wrapper) {
        List<StaffDetailAttachmentVO> attachmentVOList = wrapper.getStaffDetailAttachmentVOList();
        boolean b = this.staffDetailService.saveDetailAndStartFlow(formData,photo,contractFile,attachmentVOList);
        return new ResultMessage(b ? ResultMessage.Success : ResultMessage.Error,"");
    }

    /**
     * 完成审批更新数据并创建账号
     *
     * @param
     * @return 修改结果
     */
    @PutMapping
    public ResultMessage updateDetails(@RequestParam String id) throws Exception {
        return this.staffDetailService.updateDetails(id);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.staffDetailService.removeByIds(idList));
    }
}

